﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Crossroad.System.Admin.Model;
using Crossroad.Core.Data;
using Crossroad.Core.Exceptions;
using Crossroad.Utils.Extensions;
using Crossroad.Core.Helper;
using Crossroad.Core.Models;
using SqlSugar;

namespace Crossroad.System.Admin.Service.Impl
{
    /// <summary>
    /// 角色关联菜单操作
    /// </summary>
    public class RoleJoinMenuOptionService: Access<RoleJoinMenuOption>, IRoleJoinMenuOptionService
    {
        public RoleJoinMenuOptionService(
        )
        {
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="method"></param>
        /// <param name="loadRelevantData">是否加载关联数据</param>
        /// <returns></returns>
        public  List<RoleJoinMenuOption> GetList(
            Action<ISugarQueryable<RoleJoinMenuOption>> method,
			bool loadRelevantData = true
        )
        {
            var query = db.Queryable<RoleJoinMenuOption>();
            method(query);
            var list = query.ToList();
			if(loadRelevantData)
				Load(list);

            return list;
        }

        /// <summary>
        /// 查询详情
        /// </summary>
        /// <param name="method"></param>
        /// <param name="loadRelevantData">是否加载关联数据</param>
        /// <returns></returns>
        public RoleJoinMenuOption GetEntity(
            Action<ISugarQueryable<RoleJoinMenuOption>> method,
			bool loadRelevantData = true
		)
        {
            var query = db.Queryable<RoleJoinMenuOption>();
            method(query);
            var model = query.First();
            if (loadRelevantData)
                Load(model);

            return model;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public void CreateAt(RoleJoinMenuOption model)
        {
            if(model.roleId < 1)
                throw new LogicException("角色的菜单操作关联新增失败！角色id不能为空");
            if(model.menuId < 1)
                throw new LogicException("角色的菜单操作关联新增失败！菜单id不能为空");
            if(model.optionId.IsNullOrEmpty())
                throw new LogicException("角色的菜单操作关联新增失败！操作id不能为空");
            //if(model.value.IsNullOrEmpty())
            //    throw new LogicException("角色的菜单操作关联新增失败！权限值不能为空");

            var query = db.Queryable<RoleJoinMenuOption>();

            BeginTran();
            try
            {
                var count = query.Where(a => a.roleId == model.roleId && a.menuId == model.menuId  && a.optionId.Equals(model.optionId)).Count();
                if (count > 0)
                    throw new LogicException("角色的菜单操作关联新增失败！权限值已存在，不能重复添加");

                var rows = db.Insertable(model).ExecuteCommand();
                if (rows < 1)
                    throw new LogicException("角色的菜单操作关联新增失败！");
                CommitTran();
            }
            catch (Exception e)
            {
                RollbackTran();
                throw e;
            }
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="models"></param>
        public void CreateBatch(List<RoleJoinMenuOption> models)
        {
            BeginTran();
            try
            {
                var rows = db.Insertable(models.ToArray()).ExecuteCommand();
                if (rows < models.Count)
                    throw new LogicException("角色的菜单操作关联批量新增失败！");
                CommitTran();
            }
            catch (Exception e)
            {
                RollbackTran();
                throw e;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="source"></param>
        public void DeleteAt(RoleJoinMenuOption source)
        {
             var rows = db.Deleteable(source).ExecuteCommand();
            if (rows < 1)
                throw new LogicException("删除失败！");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="model"></param>
        public void DeleteBatch(RoleJoinMenuOption model)
        {
            if (model.roleId.IsLessOrEqual0() && model.menuId.IsLessOrEqual0() && model.optionId.IsNullOrEmpty())
                throw new LogicException("角色的菜单操作关联删除失败！组合主键不能皆为空");

            var deleteable = db.Deleteable<RoleJoinMenuOption>();
            if (!model.roleId.IsLessOrEqual0())
                deleteable.Where(f => f.roleId == model.roleId);
            if (!model.menuId.IsLessOrEqual0())
                deleteable.Where(f => f.menuId == model.menuId);
            if (!model.optionId.IsNullOrEmpty())
                deleteable.Where(f => f.optionId == model.optionId);

            BeginTran();
            try
            {
                var rows = deleteable.ExecuteCommand();
                CommitTran();
            }
            catch (Exception e)
            {
                RollbackTran();
                throw e;
            }
        }

        #region 加载关联数据

        public void Load(List<RoleJoinMenuOption> list)
        {
            if (list.IsNull())
                return;
        }

        public void Load(RoleJoinMenuOption model)
        {
            if (model.IsNull())
                return;
        }

        #endregion


    }
}
